package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.AddressAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.AddressManagementEntity;
import com.dinadi.yidi.bean.AddressManagementRequest;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/7/12  地址切换
 * Time: 18:28
 */
public class AddressActivity extends BaseActivity {
    private View view;
    private TextView tvTitle, text_title_right;
    private ImageView new_address;
    private ImageView no_designer;
    private ListView lv_product_attribute;
    private LinearLayout layout_blank_tips;
    private AddressAdapter mRefreshAdapter;
    private List<AddressManagementEntity> dataBean = new ArrayList<>();
    private List<AddressManagementEntity> dataBean2 = new ArrayList<>();
    int page = 1;
    private String userId, token;
    private boolean isSelect;

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(AddressActivity.this).inflate(R.layout.activity_address_management, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(R.string.text_address_qiehuan);
        text_title_right = (TextView) view.findViewById(R.id.text_title_right);
        text_title_right.setText("管理");
        new_address = (ImageView) view.findViewById(R.id.new_address);
        new_address.setVisibility(View.GONE);
        lv_product_attribute = (ListView) view.findViewById(R.id.lv_product_attribute);
        lv_product_attribute.setDivider(null);
        mRefreshAdapter = new AddressAdapter(AddressActivity.this, dataBean);
        lv_product_attribute.setAdapter(mRefreshAdapter);
        layout_blank_tips = (LinearLayout) view.findViewById(R.id.layout_blank_tips);
        no_designer = (ImageView) view.findViewById(R.id.no_designer);
    }

    @Override
    protected void initListener() {
        mRefreshAdapter.setOnClickListenerCallBack(new AddressAdapter.OnClickListenerCallBack() {
            @Override
            public void onClickInto(int position, String id) {
                /*跳转*/
                AddressActivity.this.finish();
                SharePreferencesHelper.getInstance(AddressActivity.this).putString("recipients", dataBean.get(position).getRecipients());
                SharePreferencesHelper.getInstance(AddressActivity.this).putString("phone", dataBean.get(position).getPhone());
                SharePreferencesHelper.getInstance(AddressActivity.this).putString("addressArea", dataBean.get(position).getAddressArea() + dataBean.get(position).getAddressDetail());
                SharePreferencesHelper.getInstance(AddressActivity.this).putString("addressId", dataBean.get(position).getAddressId());
                SharePreferencesHelper.getInstance(AddressActivity.this).putString("isRefresh", "yes");

            }
        });
        text_title_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*编辑*/
                Intent intent = new Intent(AddressActivity.this, AddressManagementActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initPullrefresh();
    }

    @Override
    protected void initData() {
        dataBean.clear();
        userId = SharePreferencesHelper.getInstance(AddressActivity.this).getString("userId");
        token = SharePreferencesHelper.getInstance(AddressActivity.this).getString("token");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        initPullrefresh();
    }

    @Override
    protected void clickEvent(View v) {

    }

    private void initPullrefresh() {
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        bean.setUserId(userId);
        bean.setPageNum(page + "");
        bean.setPageSize("100");
        CommonInterface messageInterface = new CommonInterface(AddressActivity.this, httpListener);
        messageInterface.getInterface("16011", JSON.toJSONString(bean));
    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            dataBean.clear();
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                AddressManagementRequest bean = JsonUtil.parseJsonToBean(response, AddressManagementRequest.class);
                dataBean2 = bean.getDeliveryAddressList();
                if (dataBean2.size() == 0) {
                    layout_blank_tips.setVisibility(View.VISIBLE);
                        /*换图片*/
                    no_designer.setImageResource(R.drawable.llustration_address);
                    isSelect = true;
                } else {
                    layout_blank_tips.setVisibility(View.GONE);
                }

                dataBean.addAll(bean.getDeliveryAddressList());
                mRefreshAdapter.notifyDataSetChanged();

            } else {
                closeLoadingDialog();
                toast(errMsg);
            }
        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };
}
